﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AtDevice;

namespace Mokkula
{
    public partial class StatusControl : UserControl
    {
        public StatusControl()
        {
            InitializeComponent();
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            DeviceHandler.DeviceOpened += delegate(AtDevice.AtDevice device)
            {
                RefreshInformation();
                device.GetHuaweiInterface().ModeChanged += new ModeChangedDelegate(StatusControl_ModeChanged);
            };
        }

        void StatusControl_ModeChanged(NetworkEnum network)
        {
            IGSMInterface gsmi = DeviceHandler.Get().GetGSMInterface();
            this.networkTextBox.Text = String.Format("{0} / {1} connection", gsmi.GetNetwork(), GetNetworkName(network));
        }

        private string GetNetworkName(NetworkEnum network)
        {
            switch (network)
            {
                case NetworkEnum.GprsNetwork:
                    return "GPRS";
                case NetworkEnum.HsdpaNetwork:
                    return "HSDPA";
                case NetworkEnum.HspaNetwork:
                    return "HSPA";
                case NetworkEnum.HsupaNetwork:
                    return "HSUPA";
                case NetworkEnum.EdgeNetowrk:
                    return "EDGE";
                case NetworkEnum.UmtsNetwork:
                    return "UMTS";
                case NetworkEnum.WcdmaNetwork:
                    return "WCDMA";
                default:
                    return "Unknown";
            }
        }

        private void RefreshInformation()
        {
            IGSMInterface gsmi = DeviceHandler.Get().GetGSMInterface();

            this.deviceTextBox.Text = gsmi.GetManufacturer() + " " + gsmi.GetModel();
            this.revisionTextBox.Text = gsmi.GetModelRevision();
            this.imeiTextBox.Text = gsmi.GetIMEI();
            this.imsiTextBox.Text = gsmi.GetIMSI();
            this.networkTextBox.Text = gsmi.GetNetwork() + " / [Unknown]";

            try
            {
                this.signalIndicatorControl1.Signal = gsmi.GetSignal();
            }
            catch (UnknownSignalException)
            {

            }
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            OpenDeviceConnection(this.deviceComboBox.Text);
        }

        private void deviceComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OpenDeviceConnection(this.deviceComboBox.Text);
            }
        }

        private void OpenDeviceConnection(string path)
        {
            if (DeviceHandler.Connect(path))
            {
                SetStatusConnected();
            }
        }

        private void SetStatusConnected()
        {
            this.deviceStatusLabel.Image = null;
            this.deviceStatusLabel.Text = String.Format("Device {0} opened", DeviceHandler.Get().GetPath());
        }

        private void SetStatusDisconnected()
        {
            this.deviceStatusLabel.Image = global::Mokkula.Properties.Resources.stop_16x16;
            this.deviceStatusLabel.Text = "      Device not opened";
        }

        private void StatusControl_Load(object sender, EventArgs e)
        {
            try
            {
                foreach (var item in new DirectoryInfo("/dev/").GetFiles("tty*"))
                {
                    this.deviceComboBox.Items.Add(item.FullName);
                }
            }
            catch (IOException)
            {

            }
        }
    }
}
